package com.lwj.springbootvue.config;

import com.github.pagehelper.PageInterceptor;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.List;

import java.util.Properties;

@Configuration
public class pageConfig {

    //可能存在多个连接工厂，是允许这么注入的//验证了5.3.3 和 5.1.11 两个版本都是无法正常分页的，
    @Resource//所以还是加上这个配置最好
    private List<SqlSessionFactory> sqlSessionFactoryList;
    //@PostConstruct: Bean初始化之前执行
    //@PreDestroy: 和销毁之前执行
    @PostConstruct
    public void initConfig(){
        PageInterceptor pageInterceptor=new PageInterceptor();//1.pageInterceptor
        Properties properties=new Properties();//2.properties
        // 设置数据库方言为MySQL，以便数据库操作工具类能够正确解析MySQL的SQL语法和特性
        properties.setProperty("helperDialect","mysql");
        pageInterceptor.setProperties(properties);//3.pageInterceptor设置properties

        //4.sqlSessionFactory添加pageInterceptor
        sqlSessionFactoryList.forEach(sqlSessionFactory -> sqlSessionFactory.getConfiguration().addInterceptor(pageInterceptor));
        /*sqlSessionFactoryList.forEach(new Consumer<SqlSessionFactory>() {
            public void accept(SqlSessionFactory sqlSessionFactory) {
                sqlSessionFactory.getConfiguration().addInterceptor(pageInterceptor);
            }
        });
        sqlSessionFactoryList.forEach(sqlSessionFactory -> {sqlSessionFactory.getConfiguration().addInterceptor(pageInterceptor);});*/
    }
}